package com.nervenets.web.hibernate.dao;

import com.nervenets.web.hibernate.domain.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface AddressDao extends JpaRepository<Address, Long>, JpaSpecificationExecutor<Address> {
    List<Address> findAllByUserIdOrderByDefDesc(long userId);

    @Modifying
    @Transactional
    @Query(value = "update `addresses` SET `def` = false where user_id=?1 ", nativeQuery = true)
    void updateAllAddressDefToFalse(long userId);

    @Modifying
    @Transactional
    @Query(value = "update `addresses` SET `def` = ?1 where id=?2 ", nativeQuery = true)
    void updateAddressDefStatus(boolean def, long addressId);
}
